/**
 * 热门招聘
 * @author  Jello
 * @create  2018/11/13 16:52
 * @version 1.0
 * @company 广东蓝聘科技网络有限公司
 **/
var workId = "";
var userid_sign = "";
var status = "";
var reTime = "";
var reMoney = "";
var interviewTime = "";
var mechanismId = "";
var recruitAddress = "";
var mechanismName = "";
var userToken = "";
var curPage = 1;
var size = 5;
var workTypeName = "";
var isName = "";
var token = "";
var login_status = "0";
var spinner = new Spinner(opts);

$(function() {
	userid_sign = sessionStorage.getItem("user_sign");
	userToken = sessionStorage.getItem("user_token") == null ? "" : sessionStorage.getItem("user_token");

	//菜单栏切换
	$(".info").click(function() {
		$(".tab_nav").toggle();
		$("#menubar").attr("src", $(".tab_nav").is(":hidden") ? "img/personal/arrow_bottom.png" : "img/personal/arrow_top.png");
	});

	//检测是否记住密码
	var loginSign = localStorage.getItem("phone_local");
	var pwdSign = localStorage.getItem("pwd_local");
	if(pwdSign) {
		$("#loginPhone").val(loginSign);
		$("#loginPwd").val($.base64.decode(pwdSign));
		$("#checkbox").attr("checked", true);
	} else {
		$("#loginPhone").val(loginSign);
		$("#loginPwd").val("");
		$("#checkbox").attr("checked", false);
	}

	//获取招聘id
	workId = getParam("id");

	//查询招聘详情
	var param = {};
	param.workId = workId;
	queryWorkDetail(param, function(resultObj) {
		var detailObj = resultObj.data;
		var postType = detailObj.postType; //工种0正式1小时

		status = detailObj.status; //招工状态0正在招工 1已经招满
		reTime = detailObj.reTime; //返费时间（单位：月）
		reMoney = detailObj.reMoney; //返费金额
		interviewTime = detailObj.interviewTime; //面试时间
		mechanismId = detailObj.mechanismId; //企业id
		recruitAddress = detailObj.recruitAddress; //面试地址
		mechanismName = detailObj.mechanismName; //公司名称
		workTypeName = detailObj.workTypeName;

		if(status == 1) {
			$("#fill_btn").show();
			$("#fill_btn").attr("disabled", true);
		} else {
			$("#apply_btn").show();
		}
		var salary = "";
		if(postType == 1) {
			salary = detailObj.workMoney + "元/小时";
		} else {
			salary = detailObj.wageRange + "元/月";
		}

		var lend = sessionStorage.getItem("lendType");
		if(lend == 1) {
			var lendType = "可借支";
			$("#lendType").show();
			$("#lendType").html(lendType);
		}
		$("#money").html(salary);
		$("#post").html(detailObj.postName); //工种名称
		$("#mechanismName").html(detailObj.mechanismName);
		$("#mechanismScore").html(detailObj.mechanismScore); //企业评分
		$("#maxNumber").html(detailObj.maxNumber); //需求人数
		$("#workTypeName").html(workTypeName); //岗位名称
		$("#applyNumber").html(detailObj.applyNumber); //已报名数
		$("#mechanismDetails").html(detailObj.mechanismDetails); //公司简介
		$("#mechanismAddress").html(detailObj.mechanismAddress); //公司地址

		var keyList = ""; //关键字
		if(detailObj.key != null) {
			var html = "";
			keyList = detailObj.key.split("|");
			for(var j = 0; j < keyList.length; j++) {
				var htmlKey = '<span>' + keyList[j] + '</span>';
				html += htmlKey;
			}
			$("#key").append(html);
		}

		$("#workDemand").html(detailObj.workDemand); //入职要求
		$("#workSalary").html(detailObj.workSalary); //薪资福利
		$("#eatSleep").html(detailObj.eatSleep); //住宿餐饮
		$("#workTime").html(detailObj.workTime); //工作性质
		$("#workKnow").html(detailObj.workKnow); //面试材料
		$("#remarks").html(detailObj.remarks); //其他说明
	});

	if(userid_sign) {
		$("#nickname").html(sessionStorage.getItem("nickname"));
		$(".person_header").show();
		login_status = "1";
		//查询是否报名、收藏
		var param = {};
		param.workId = workId;
		param.token = userToken;
		param.login_status = login_status;
		queryIsApplyOrCollect(param, function(resultObj) {
			var isApply = resultObj.isApply; //0已报名1未报名
			var isCollect = resultObj.isCollection; //0已收藏 1未收藏
			var role = sessionStorage.getItem("role"); //判断角色，禁止报名
			isName = resultObj.userName;
			if(role == 4 || role > 6) {
				$("#forbid_btn").show();
				$("#forbid_btn").attr("disabled", true);
			} else {
				if(status == 1) {
					$("#fill_btn").show();
					$("#fill_btn").attr("disabled", true);
				} else if(isApply == 0) {
					$("#cancel_btn").show();
				} else {
					$("#apply_btn").show();
				}
			}
			if(isCollect == 0) {
				$(".start").attr("src", "img/work/start2.png")
				$(".collect_txt").html("已收藏");
			}
		});
	} else {
		$(".header_right").show();
	}

	//入职报名弹出层
	$("#apply_btn").click(function() {
		if(userid_sign) {
			if(isName) {
				var param = {};
				param.reTime = reTime;
				param.reMoney = reMoney;
				param.identy = "";
				param.interviewTime = interviewTime;
				param.mechanismId = mechanismId;
				param.userName = isName;
				param.workId = workId;
				param.recruitAddress = recruitAddress;
				param.mechanismName = mechanismName;
				param.type = 0;
				param.token = userToken;
				entryApply(param, function(resultObj) {
					$(".applybox").hide();
					$(document).unbind("scroll");
					if(resultObj.code == 0 && resultObj.data != null) {
						$(".victorbox").show();
						setTimeout(function() {
							$(".victorbox").hide();
							window.location.reload();
						}, 800);
					} else if(resultObj.code == 10002) {
						window.location.href = "login.html";
					} else {
						$(".warnbox").show();
						$(".warntext").html(resultObj.msg);
						return;
					}
				});
			} else {
				stopScroll();
				$(".applybox").show();
			}
		} else { //登录弹出层
			stopScroll();
			$(".loginBox").show();
		}
	})

	$(".no").click(function() {
		$(".applybox").hide();
		$(".error").hide();
		$(document).unbind("scroll");
	})

	$("#applyname").click(function() {
		$(".error").hide();
	})
	$(".cover").click(function() {
		$(document).unbind("scroll");
		$(".warnbox").hide();
	})

	//入职报名确定
	$("#apply_btn2").click(function() {
		var name = $("#applyname").val();
		var flag = checkApplyName(name);
		if(flag) {
			var param = {};
			param.reTime = reTime;
			param.reMoney = reMoney;
			param.identy = "";
			param.interviewTime = interviewTime;
			param.mechanismId = mechanismId;
			param.userName = name;
			param.workId = workId;
			param.recruitAddress = recruitAddress;
			param.mechanismName = mechanismName;
			param.type = 0;
			param.token = userToken;
			entryApply(param, function(resultObj) {
				$(".applybox").hide();
				$(document).unbind("scroll");
				if(resultObj.code == 0 && resultObj.data != null) {
					$(".victorbox").show();
					setTimeout(function() {
						$(".victorbox").hide();
						window.location.reload();
					}, 800);
				} else if(resultObj.code == 10002) {
					window.location.href = "login.html";
				} else {
					$(".warnbox").show();
					$(".warntext").html(resultObj.msg);
					return;
				}
			});
		}
	})

	//取消报名
	$("#cancel_btn").click(function() {
		$(".intervie_box").show();
		stopScroll();
	})
	$("#box_yes").click(function() {
		//取消报名接口上送
		var param = {};
		param.workId = workId;
		param.token = userToken;
		cancelApply(param, function(resultObj) {
			if(resultObj.code == 0 && resultObj.data != null) {
				$(".intervie_box").hide();
				$(".errorbox").show();
				$(".errortext").html("取消成功");
				setTimeout(function() {
					$(".errorbox").hide();
					$(document).unbind("scroll");
					window.location.reload();
				}, 800);
			} else if(resultObj.code == 10002) {
				window.location.href = "login.html";
			} else {
				$(".errorbox").show();
				$(".errorimg").attr("src", "img/icon/error.png")
				$(".errortext").val("取消失败");
				return;
			}
		});
	})
	$("#box_cancel").click(function() {
		$(document).unbind("scroll");
		$(".intervie_box").hide();
	})

	$(".warnbtn").click(function() {
		$(".victorbox").hide();
		$(document).unbind("scroll");
	})

	//收藏
	$("#collect_btn").click(function() {
		if(userid_sign) {
			var param = {};
			param.type = 1;
			param.id = workId;
			param.token = userToken;
			collectJob(param, function(resultObj) {
				stopScroll();
				if(resultObj.code == 0 && resultObj.data != null) {
					if(resultObj.data == 1) {
						$(".errortext").html("取消成功");
					} else {
						$(".errortext").html("收藏成功");
					}
					$(".errorbox").show();
					setTimeout(function() {
						$(".errorbox").hide();
						window.location.reload();
					}, 800);
				} else if(resultObj.code == 10002) {
					window.location.href = "login.html";
				} else {
					$(".errorbox").show();
					$(".errortext").html("收藏失败");
					return;
				}
			})
		} else {
			stopScroll();
			$(".loginBox").show();
		}
	})

	$(".errcover").click(function() {
		$(".errorbox").hide();
		$(document).unbind("scroll");
	})

	findSimilarJob(curPage);

	//登录、注册弹出层
	$("#loginbtn").click(function() {
		stopScroll();
		$(".loginBox").show();
	})
	$("#registbtn").click(function() {
		stopScroll();
		$(".registBox").show();
	})
	//登录、注册遮罩层
	$(".logincover").click(function() {
		$(document).unbind("scroll");
		$(".loginBox").hide();
	})
	$(".registcover").click(function() {
		$(document).unbind("scroll");
		$(".registBox").hide();
	})

	//登录监听
	$("#loginPhone").on('input propertychange', function() {
		var phone = $(this).val();
		checkPhone(phone, "error2", "error1");
	})
	$("#loginPwd").on('input propertychange', function() {
		var pwd = $(this).val();
		checkPwd(pwd, "error2", "error1");
	})
	$("#loginPhone").click(function() {
		$(this).css("background", 'url(img/icon/phone.png) no-repeat 12px center');
	})
	$("#loginPwd").click(function() {
		$(this).css("background", 'url(img/icon/pwd.png) no-repeat 12px center');
	})
	$("#loginPhone").blur(function() {
		$("#error2").html("");
		$("#error1").hide();
		$(this).css("background", '');
	})
	$("#loginPwd").blur(function() {
		$("#error2").html("");
		$("#error1").hide();
		$(this).css("background", '');
	})
	//注册监听
	$("#registPhone").on('input propertychange', function() {
		var phone = $(this).val();
		checkPhone(phone, "error4", "error3");
	})
	$("#registPwd").on('input propertychange', function() {
		var pwd = $(this).val();
		checkPwd(pwd, "error4", "error3");
	})
	$("#registPhone").click(function() {
		$(this).css("background", 'url(img/icon/phone.png) no-repeat 12px center');
	})
	$("#registPwd").click(function() {
		$(this).css("background", 'url(img/icon/pwd.png) no-repeat 12px center');
	})
	$("#registPhone").blur(function() {
		$("#error4").html("");
		$("#error3").hide();
		$(this).css("background", '');
	})
	$("#registPwd").blur(function() {
		$("#error4").html("");
		$("#error3").hide();
		$(this).css("background", '');
	})
	//验证码监听
	$("#code").click(function() {
		$(this).css("background", 'url(img/icon/code.png) no-repeat 12px center');
	})
	$("#code").blur(function() {
		$("#error4").html("");
		$("#error3").hide();
		$(this).css("background", '');
	})

	//登录
	$("#loginbox_btn").click(function() {
		var phone = $("#loginPhone").val();
		var pwd = $("#loginPwd").val();
		if(checkPhone(phone, "error2", "error1") && checkPwd(pwd, "error2", "error1")) {
			//登录接口上送
			var param = {};
			param.phone = phone;
			param.pwd = pwd;
			login(param, function(resultObj) {
				if(resultObj.data.msg == "null") {
					$("#error2").html("用户不存在");
					$("#error1").show();
				} else if(resultObj.data.msg == "error") {
					$("#error2").html("用户名或密码错误");
					$("#error1").show();
				} else {
					rememberPwd(); //回调记住密码
					sessionStorage.setItem("user_token", resultObj.data.userToken); //存储token
					sessionStorage.setItem("user_sign", resultObj.data.id); //登录返回userid，去验证是否登录
					login_status = "1";
					//获取个人资料
					var param = {};
					param.token = resultObj.data.userToken;
					getUserMaterial(param, function(data) {
						sessionStorage.setItem("role", data.role);
						sessionStorage.setItem("nickname", data.user_name);
						window.location.reload();
					});
				}
			})
		}
	});

	//获取验证码
	$("#code_btn").click(function() {
		var phoneNum = $("#registPhone").val(); //手机号
		var pwd = $("#registPwd").val(); //密码

		var checkValue = (checkPhone(phoneNum, "error4", "error3") && checkPwd(pwd, "error4", "error3"));
		if(checkValue) {
			//获取验证码接口上送
			var param = {};
			param.phone = phoneNum;
			param.type = 0;
			gainCode(param, function(resultObj) {
				if(resultObj.code == 0) {
					token = resultObj.data; //获取验证码成功后返回的token
					//开启验证码定时器
					for(i = 1; i <= 60; i++) {
						if(i == 1) {
							window.setTimeout("countDown(" + i + ")");
						} else {
							window.setTimeout("countDown(" + i + ")", (i - 1) * 1000);
						}
					}
				} else {
					$("#error4").html(resultObj.msg);
					$("#error3").show();
					return;
				}
			});
		}
	})

	//注册
	$("#registbox_btn").click(function() {
		var phoneNum = $("#registPhone").val(); //手机号
		var pwd = $("#registPwd").val(); //密码
		var code = $("#code").val(); //验证码
		var flag = (checkPhone(phoneNum, "error4", "error3") && checkPwd(pwd, "error4", "error3") && checkCode(code));
		if(flag) {
			var checked = $("#registcheck").prop("checked"); //协议checkbox
			if(!checked) {
				$("#error4").html("请阅读并勾选协议");
				$(".errorMsg2").show();
				return;
			}
			//校验验证码接口上送
			var param = {};
			param.phone = phoneNum;
			param.type = 0;
			param.code = code;
			param.token = token;
			mateCode(param, function(mateObj) {
				if(mateObj.code == 0) {
					//注册接口上送
					var param = {};
					param.phone = phoneNum;
					param.pwd = pwd;
					regist(param, function() {
						$(".registBox").hide();
						$(".errortext").html("注册成功");
						$(".errorbox").show();
						$(".errorMsg2").hide();
						setTimeout(function() {
							$(".errorbox").hide();
							window.location.href = "regist.html";
						}, 800);
					})
				} else {
					$("#error4").html(mateObj.msg);
					$("#error3").show();
					return;
				}
			})
		}
	})

	//免费登录
	$("#free_login").click(function() {
		window.open("regist.html");
	})

	//免费注册
	$("#free_regist").click(function() {
		window.open("regist.html?type=1");
	})

	//根据关键字或公司查询
	$("#searchJob").click(function() {
		var companyName = $("#companyKey").val();
		sessionStorage.setItem("keyword", companyName);
		window.location.href = "applyjob.html";
	})

	$("#companyKey").keyup(function(event) {
		if(event.keyCode == 13) {
			var companyName = $("#companyKey").val();
			sessionStorage.setItem("keyword", companyName);
			window.location.href = "applyjob.html";
		}
	});

	//客服中心
	$("#service_btn1").click(function() {
		window.open("tencent://message/?uin=1423335266&Site=lanpin123.com&Menu=yes");
	})
	$("#service_btn2").click(function() {
		window.open("tencent://message/?uin=2028048473&Site=lanpin123.com&Menu=yes");
	})
	$("#service_btn3").click(function() {
		window.open("tencent://message/?uin=3341118684&Site=lanpin123.com&Menu=yes");
	})
})

/**
 * 校验真实姓名
 * @param {Object} name
 */
function checkApplyName(name) {
	var reg = /^[\u2E80-\u9FFF]+$/; //中文
	if(name == null || name == "") {
		$(".error_txt").html("请输入您的姓名");
		$(".error").show();
		return;
	} else if(!reg.test(name)) {
		$(".error_txt").html("请输入您的中文姓名");
		$(".error").show();
		return;
	}
	return true;
}

/**
 * 跳转首页
 */
function indexPage() {
	if(userid_sign) {
		window.location.href = "index.html";
	} else {
		window.location.href = "login.html";
	}
}

/**
 * 跳转求职页面
 */
function applyPage() {
	window.location.href = "applyjob.html";
}

/**
 * 跳转热门招聘
 */
function favorPage() {
	window.location.href = "favorjob.html";
}

/**
 * 查询相似职位
 * @param {Object} curPage
 */
function findSimilarJob(curPage) {
	$.ajax({
		type: "get",
		url: serverUrl + "/workapi/get_work_list_api?userToken=" + userToken + "&status=" + login_status + "&page=" + curPage + "&size=" + size + "&type=1" + "&workTypeName=" + workTypeName,
		cache: false,
		async: false,
		contentType: "application/json; charset=utf-8",
		timeout: asktime,
		dataType: "json",
		crossDomain: true,
		success: function(data) {
			var resultObj = eval(data);
			$(".similar_job").empty();
			if(resultObj.code == 0 && resultObj.data.list.length != 0) {
				var key = "";
				for(var i = 0; i < resultObj.data.list.length; i++) {
					var workList = resultObj.data.list;
					var lend_type = workList[i].lendType;
					if(workList[i].key == null) {
						key = "";
					} else {
						key = workList[i].key; //关键字
					}
					var resemble_html = '<div class="resemble" onclick="querySimilarDetail(' + workList[i].id + ',' + lend_type + ')"><div class="resemble_img"><img src="' + workList[i].mechanismUrl + '"/></div>' +
						'<div class="resemble_con"><span class="company">' + workList[i].mechanismName + '</span>' +
						'<span class="companykey">' + key + '</span></div></div>';
					$(".similar_job").append(resemble_html);
				}
			} else if(resultObj.code == 10002) {
				window.location.href = "login.html";
			}
		}
	});
}

/**
 * 查询招聘详情
 */
function querySimilarDetail(id, lend) {
	sessionStorage.setItem("lendType", lend);
	window.location.href = "jobdetail.html?id=" + id;
}

/**
 * 验证码倒计时
 * @param {Object} count 倒数
 */
function countDown(count) {
	if(count == 60) {
		$("#code_btn").html("获取验证码");
		$("#code_btn").attr('disabled', false);
	} else {
		left_time = 60 - count;
		$("#code_btn").attr('disabled', true);
		$("#code_btn").html(left_time + "秒重新发送");
	}
}

/**
 * 记住密码
 */
function rememberPwd() {
	var phoneNum = $("#loginPhone").val(); //手机号
	var pwd = $("#loginPwd").val(); //密码
	var checked = $('#checkbox').is(':checked');
	if(checked) { //选中记住密码,永久存储本地
		localStorage.setItem("phone_local", phoneNum);
		localStorage.setItem("pwd_local", $.base64.encode(pwd));
	} else {
		localStorage.removeItem("pwd_local");
	}
}

/**
 * 校验手机号
 */
function checkPhone(phone, showTxt, showId) {
	var reg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
	if(phone == "") {
		$("#" + showTxt).html("手机号码不能为空");
		$("#" + showId).show();
		return;
	} else if(!reg.test(phone)) {
		$("#" + showTxt).html("手机号码格式错误");
		$("#" + showId).show();
		return;
	} else {
		$("#" + showTxt).html("");
		$("#" + showId).hide();
	}
	return true;
}

/**
 * 校验密码
 * @param {Object} pwd
 */
function checkPwd(pwd, showTxt, showId) {
	if(pwd == "") {
		$("#" + showTxt).html("密码不能为空");
		$("#" + showId).show();
		return;
	} else if(pwd.indexOf(" ") >= 0) {
		$("#" + showTxt).html("密码中不能有空格字符");
		$("#" + showId).show();
		return;
	} else if((/[\u4e00-\u9fa5]+/).test(pwd)) {
		$("#" + showTxt).html("密码不能包含中文");
		$("#" + showId).show();
		return;
	} else if(pwd.length < 6 || pwd.length > 16) {
		$("#" + showTxt).html("密码长度为6-16位");
		$("#" + showId).show();
		return;
	} else {
		$("#" + showTxt).html("");
		$("#" + showId).hide();
	}
	return true;
}

/**
 * 检验验证码
 * @param {Object} code
 */
function checkCode(code) {
	var reg = /^[0-9]*$/;
	if(code == "") {
		$("#error4").html("验证码不能为空");
		$("#error3").show();
		return;
	} else if(!reg.test(code)) {
		$("#error4").html("验证码格式不正确");
		$("#error3").show();
		return;
	}
	return true;
}